package com.css.dzj.dzjap.modules.force.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.force.entity.ApJgxxForceEntity;
import com.css.dzj.dzjap.modules.force.service.ForceService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/force")
public class ForceController {

    @Autowired
    private ForceService forceService;
    @Autowired
    BaseService baseService;
    /**q
     * 添加(行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/addForce")
    public R addForce(@RequestBody ApJgxxForceEntity apJgxxForceEntity){
        String msg = forceService.addForce(apJgxxForceEntity);
        baseService.logSuccess(3,"添加信息成功","2");
        return R.ok().put("data", msg);
    }
    /**
     * 通过ID删除(行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/delForce")
    public R delForce(@RequestParam("forceId") String forceId){
        if(forceService.delForce(forceId)){
            baseService.logSuccess(3,"删除信息成功","2");
            return R.ok("操作成功!");
        }else{
            baseService.logFailed(3,"删除信息失败","2");
            return  R.error("操作失败!");
        }
    }
    /**
     * 修改（行政强制行为）监管信息信用表
     */
    @PostMapping(value = "/upForce")
    public R updateForce(@RequestBody ApJgxxForceEntity apJgxxForceEntity){
        ApJgxxForceEntity force = forceService.getForce(apJgxxForceEntity.getForceId());
        BeanUtil.copyBeanNotNullToBean(apJgxxForceEntity, force);
        forceService.updateForce(force);
        baseService.logSuccess(3,"修改信息成功","2");
        return R.ok();
    }
    /**
     * 通过ID查询（行政强制行为）监管信息信用表
     */
    @GetMapping(value = "/getForce")
    public R getForce(@RequestParam("forceId") String forceId){
        ApJgxxForceEntity force = forceService.getForce(forceId);
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data", force);
    }
    /**
     *分页查询所有其他行为信息表
     */
    @GetMapping(value = "/getAllForce")
    public R getAllForce(@RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults results = forceService.getAllForce(pageRequest);
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data", results);
    }
    /**
     * 条件查询
     */
    @GetMapping(value ="/getAllContation")
    public R getAllConOther(@RequestParam String superviseId,
                            @RequestParam(required = false) String forceActionName,
                            @RequestParam(required = false) String forceActionCode,
                            @RequestParam(required = false) String submittedDate,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        baseService.logSuccess(3,"查询信息成功","2");
        return R.ok().put("data", forceService.getAllContation(pageRequest,superviseId,forceActionName,forceActionCode,submittedDate));
    }

    /***
     * 获取强制行为
     * @return
     */
    @GetMapping("/force")
    public R countForce(){
        HashMap count=forceService.getCount();
        return R.ok().put("count",count);
    }
    /***
     * 获取强制行为
     * @return
     */
    @GetMapping("/forceyear")
    public R countForceByYear(String year){
        HashMap count=forceService.getCountByYear(year);
        return R.ok().put("count",count);
    }
    /***
     * 获取当前登陆人的机构的强制行为
     * @param request
     * @return
     */
    @GetMapping("/getforce")
    public R getOpForce(HttpServletRequest request){
        Long count=forceService.getOpCheck(request);
        return R.ok().put("count",count);
    }

   /**
   *@Author weitao
   *@Date 14:58 2020/4/29
    * 多条件查询强制行为
   */
   @GetMapping(value ="/getWtContation")
   public R getWtContation(@RequestParam String superviseId,
                           @RequestParam(required = false) String forceActionName,
                           @RequestParam(required = false) String implementInstitution,
                           @RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize){

       PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
       QueryResults<ApJgxxForceEntity> foreResult = forceService.getWtContation(superviseId,forceActionName,implementInstitution,pageRequest);
       return R.ok().put("data", foreResult);
   }

    /**
     * 根据参数导出数据到Excel
     */
    @GetMapping(value ="/getWtForceList")
    public List<ApJgxxForceEntity> getWtContation(@RequestParam String superviseId,
                            @RequestParam(required = false) String forceActionName,
                            @RequestParam(required = false) String implementInstitution){

       List<ApJgxxForceEntity> forceList = forceService.forceList(superviseId,forceActionName,implementInstitution);

        return forceList;
    }

    /**
     *根据ids查询导出数据
     */
    @GetMapping(value ="/getWtForceIds")
    public List<ApJgxxForceEntity> queryByIds(@RequestParam String ids){

        List<ApJgxxForceEntity> forceList = forceService.queryByIds(ids);
        return forceList;
    }

   /**
   *@Author weitao
   *@Date 15:29 2020/4/29
    * 多条件导出
   */
   @GetMapping("/export")
   public R export(HttpServletResponse response,
                   @RequestParam String superviseId,
                   @RequestParam(required = false) String forceActionName,
                   @RequestParam(required = false) String implementInstitution) {
       try {
           List<ApJgxxForceEntity> foreResult = forceService.forceList(superviseId,forceActionName,
                   implementInstitution);
           forceService.exportForceToExcel(response, foreResult);
           return R.ok();
       } catch (Exception e) {
           e.printStackTrace();
           return R.error(e.getMessage());
       }
   }

    /**
     *导出选中行数据
     */
    @GetMapping("/exportByIds")
    public R exportByIds(HttpServletResponse response, @RequestParam String ids) {
        try {
            List<ApJgxxForceEntity> forceList = forceService.queryByIds(ids);
            forceService.exportForceToExcel(response, forceList);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     *@Author weitao
     *@Date 14:39 2020/4/29
     * 统计数量
     */
    @GetMapping("/forceCount")
    public R forceCount(){

        try {
            String forceCount = forceService.getForceCount();
            return R.ok().put("data",forceCount);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    /**
     * 通过ID查询（行政强制行为）监管信息信用表
     */
    @GetMapping(value = "/getForceID")
    public R getForceID(@RequestParam("forceId") String forceId){
        ApJgxxForceEntity force = forceService.getForce(forceId);
        return R.ok().put("data", force);
    }


}
